#include "task_manager.h"
#include "account_manager.h"
#include <cstddef> 
#include <string>
#include <iostream>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <memory>

using namespace std;

void printMainMenu() {
    cout << "\n===== 任务管理系统 =====" << endl;
    cout << "1. 登录" << endl;
    cout << "2. 注册" << endl;
    cout << "3. 退出" << endl;
    cout << "请选择: ";
}

void printTaskMenu() {
    cout << "\n===== 任务管理 =====" << endl;
    cout << "1. 添加任务" << endl;
    cout << "2. 查看所有任务" << endl;
    cout << "3. 查看某天任务" << endl;
    cout << "4. 删除任务" << endl;
    cout << "5. 退出登录" << endl;
    cout << "请选择: ";
}

time_t getTimeInput(const string& prompt) {
    cout << prompt << "(格式: YYYY-MM-DD HH:MM): ";
    
    int year, month, day, hour, minute;
    char sep1, sep2, sep3;
    
    string line;
    getline(cin, line);
    istringstream iss(line);
    
    if (!(iss >> year >> sep1 >> month >> sep2 >> day >> hour >> sep3 >> minute) ||
        sep1 != '-' || sep2 != '-' || sep3 != ':') {
        throw runtime_error("无效的时间格式");
    }
    
    tm tm = {0};
    tm.tm_year = year - 1900;
    tm.tm_mon = month - 1;
    tm.tm_mday = day;
    tm.tm_hour = hour;
    tm.tm_min = minute;
    tm.tm_isdst = -1;
    
    return mktime(&tm);
}

Task::Priority getPriorityInput() {
    cout << "优先级 (1-低, 2-中, 3-高) [默认中]: ";
    string input;
    getline(cin, input);
    
    if (input.empty()) return Task::MEDIUM;
    
    switch (stoi(input)) {
        case 1: return Task::LOW;
        case 2: return Task::MEDIUM;
        case 3: return Task::HIGH;
        default: return Task::MEDIUM;
    }
}

Task::Category getCategoryInput() {
    cout << "分类 (1-学习, 2-娱乐, 3-生活) [默认生活]: ";
    string input;
    getline(cin, input);
    
    if (input.empty()) return Task::LIFE;
    
    switch (stoi(input)) {
        case 1: return Task::STUDY;
        case 2: return Task::ENTERTAINMENT;
        case 3: return Task::LIFE;
        default: return Task::LIFE;
    }
}

int main() {
    AccountManager accountManager("accounts.dat");
    unique_ptr<TaskManager> taskManager;
    string currentUser;
    
    while (true) {
        printMainMenu();
        string choice;
        getline(cin, choice);
        
        if (choice == "1") { // 登录
            cout << "用户名: ";
            string username;
            getline(cin, username);
            
            cout << "密码: ";
            string password;
            getline(cin, password);
            
            if (accountManager.authenticate(username, password)) {
                currentUser = username;
                taskManager = make_unique<TaskManager>(username + "_tasks.txt");
                cout << "登录成功!" << endl;
                loadUserTasks(username);
                
                while (true) {
                    printTaskMenu();
                    string taskChoice;
                    getline(cin, taskChoice);
                    
                    if (taskChoice == "1") { // 添加任务
                        try {
                            string name;
                            cout << "任务名称: ";
                            getline(cin, name);
                            
                            time_t startTime = getTimeInput("开始时间");
                            time_t reminder = getTimeInput("提醒时间");
                            
                            Task::Priority priority = getPriorityInput();
                            Task::Category category = getCategoryInput();
                            
                            Task task(0, name, startTime, priority, category, reminder);
                            
                            if (taskManager->addTask(task)) {
                                cout << "任务添加成功!" << endl;
                            } else {
                                cout << "添加失败: 任务时间冲突或名称重复" << endl;
                            }
                        } catch (const exception& e) {
                            cerr << "错误: " << e.what() << endl;
                        }
                    } 
                    else if (taskChoice == "2") { // 查看所有任务
                    
                        // 加载用户任务
                  if (loadUserTasks(username + "_tasks.txt")) {
                    // 显示加载的任务
                  cout << "\n Loaded Tasks:\n";
                  xianshi(taskList);
                    } 
                    else if (taskChoice == "3") { // 查看某天任务
                        try {
                            taskManager->viewScheduleByDateRange();
                        } catch (const exception& e) {
                            cerr << "错误: " << e.what() << endl;
                        }
                    } 
                    else if (taskChoice == "4") { // 删除任务
                        cout << "输入要删除的任务ID: ";
                        int id;
                        cin >> id;
                        cin.ignore();
                        
                        if (taskManager->deleteTask(id)) {
                            cout << "任务删除成功!" << endl;
                        } else {
                            cout << "删除失败: 未找到该ID的任务" << endl;
                        }
                    } 
                    else if (taskChoice == "5") { // 退出登录
                        break;
                    } 
                    else {
                        cout << "无效选择!" << endl;
                    }
                }
            } else {
                cout << "用户名或密码错误!" << endl;
            }
        } 
        else if (choice == "2") { // 注册
            cout << "用户名: ";
            string username;
            getline(cin, username);
            
            cout << "密码: ";
            string password;
            getline(cin, password);
            
            if (accountManager.createAccount(username, password)) {
                cout << "注册成功!" << endl;
            } else {
                cout << "注册失败!" << endl;
            }
        } 
        else if (choice == "3") { // 退出
            break;
        } 
        else {
            cout << "无效选择!" << endl;
        }
    }
    
    
    return 0;
}
